package b.utils.entity

import akka.NotUsed
import com.lightbend.lagom.scaladsl.persistence.PersistentEntity

/**
  * 贲国称 于 2016/12/26 创建。
  */
trait T底层Command
case class CC序号申请Command(id: String) extends T底层Command with PersistentEntity.ReplyType[CC序号申请应答]
case class CC多个序号申请Command(id: String, several: Int) extends T底层Command with PersistentEntity.ReplyType[Array[Long]]
case class CC序号申请应答(v序号: Long)
class C流水号PersistentEntity extends ACPersistentEntity {

  override type Command = T底层Command
  override type Event = NotUsed
  override type State = C流水号产生器

  override def initialState: State = new C流水号产生器(1, 1, 1L)

  override def behavior: Behavior = {
    Actions().onReadOnlyCommand[CC序号申请Command, CC序号申请应答] {
      case (cmd: CC序号申请Command, ctx, state) => ctx.reply(CC序号申请应答(state.nextId()))
    }.onReadOnlyCommand[CC多个序号申请Command, Array[Long]] {
      case (cmd: CC多个序号申请Command, ctx, state) => ctx.reply(Array.fill[Int](cmd.several)(0).map(_ => state.nextId()))
    }
  }

}
